<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>AdminLTE 3 | Log in</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" th:href="@{/static/plugins/fontawesome-free/css/all.min.css}">
  <!-- icheck bootstrap -->
  <link rel="stylesheet" th:href="@{/static/plugins/icheck-bootstrap/icheck-bootstrap.min.css}">
  <!-- Theme style -->
  <link rel="stylesheet" th:href="@{/static/dist/css/adminlte.min.css}">
</head>
<body class="hold-transition login-page">
<div class="login-box">
  <div class="login-logo">
    <a href="#"><b>Admin</b>LTE</a>
  </div>
  <!-- /.login-logo -->
  <div class="card">
    <div class="card-body login-card-body">
      <p class="login-box-msg">登录</p>

      <form id="loginform">
        <div class="input-group mb-3">
          <input type="text" class="form-control"
                 id="username" name="username"
                 placeholder="Username">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-envelope"></span>
            </div>
          </div>
        </div>
        <div class="input-group mb-3">
          <input type="password" class="form-control"
                 id="password" name="password"
                 placeholder="Password">
          <div class="input-group-append">
            <div class="input-group-text">
              <span class="fas fa-lock"></span>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-8">
            <div class="icheck-primary">
              <input type="checkbox" id="remember">
              <label for="remember">
                Remember Me
              </label>
            </div>
          </div>
          <!-- /.col -->
          <div class="col-4">
            <button type="submit"
                    id="submit"
                    class="btn btn-primary btn-block">Sign In</button>
          </div>
          <!-- /.col -->
        </div>
      </form>
      <!-- /.social-auth-links -->

      <p class="mb-1">
        <a href="forgot-password.html">I forgot my password</a>
      </p>
      <p class="mb-0">
        <a href="register.html" class="text-center">Register a new membership</a>
      </p>
    </div>
    <!-- /.login-card-body -->
  </div>
  </div>
</div>
</div>
<!-- /.login-box -->

<!-- jQuery -->
<script th:src="@{/static/js/jquery/jquery-3.7.1.min.js}"></script>
<!--<script th:src="@{/static/plugins/jquery/jquery.min.js}"></script>-->
<!-- Bootstrap 4 -->
<script th:src="@{/static/plugins/bootstrap/js/bootstrap.bundle.min.js}"></script>
<!-- AdminLTE App -->
<script th:src="@{/static/dist/js/adminlte.min.js}"></script>
<script th:src="@{/static/js/layer/layer.js}"></script>
<script>
  // 定位到提交按钮
  $('#submit').click(function(event) {

    var username = $('#username').val();
    var password = $('#password').val();
    event.preventDefault();
    console.log(username + ", " + password);
    // 非空验证
    if (username === '' || password === '') {
      layer.msg("用户名或密码为空",
              {icon: 2, time: 1000}
      );
      return;
    }

    // ajax发送至后台处理
    $.ajax({
      url: '[[@{/}]]' + 'login',
      type: 'POST',
      //返回类型
      dataType: 'json',
      // 发送数据的格式和编码
      contentType: "application/json; charset=utf-8",
      // 封装数据并序列化
      data: JSON.stringify({
        'username': username,
        // 这里可以对密码进行加密
        'password': password
      }),
      success: function (data) {
        if (data.code == 200) {
          alert(data.code + ", " + data.operationCode + ", " + data.message) ;

          if (data.operationCode === "success") {
            // 登录成功，则跳转
            layer.msg('登录成功',
                    {
                      icon: 1, time: 1000
                    },
                    function () {
                    alert("to be junk.")
                      // 跳转到后台主页
                      window.location.href = "[[@{/}]]" + "backend/index";
                    }
            )
          } else {
            layer.msg(data.message, {
              icon: 2, time: 2000
            })
          }
        } else {
          layer.msg(data.message,
                  {
                    icon: 2, time: 2000
                  }
          )
        }
      },
      error: function () {

      }
    })
  })

</script>



</body>
</html>
